package com.penghk.hangzhouhouseprice.config;

import com.penghk.hangzhouhouseprice.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

@Configuration
public class MailConfig {

    @Value("${mail.host}")
    String host;

    @Value("${mail.port}")
    String port;

    @Value(("${mail.username}"))
    String userName;

    @Value(("${mail.password}"))
    String password;

    @Value("${secretkey}")
    String secretKey;

    @Bean
    public JavaMailSenderImpl javaMailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setPort(Integer.parseInt(port));
        javaMailSender.setProtocol("smtps");
        javaMailSender.setUsername(userName);
        password = EncryptUtil.decryptByAES(password , secretKey);
        javaMailSender.setPassword(password);
        Properties properties = new Properties();
        properties.setProperty("mail.smtps.auth" , "true");
        properties.setProperty("mail.smtp.ssl.enable" , "true");
        properties.setProperty("mail.transport.protocol" , "smtps");
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }

    @Bean
    public SimpleMailMessage simpleMailMessage() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        return simpleMailMessage;
    }
}
